/*
+ --------------------------------------------------- +
|  Ineo Article Manager - Version 2.x
|  Copyright 2006-2007, Dan Cryer and William McGann
+ --------------------------------------------------- +

Ineo Article Manager is free software; you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License as published by
the Free Software Foundation.

Ineo Article Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Ineo; see the file License.txt.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/

window.onload = ineoGlobalInit;

var staro1; var staro2; var staro3; var staro4; var staro5;

/**
* Global initialization - set up event handlers, etc.
*/
function ineoGlobalInit()
{
	// Global Event Handlers:
	if( document.getElementById('ineo-quicksearch-form') )
	{
		document.getElementById('ineo-quicksearch-form').onsubmit = function() 
		{
			ineoQuickSearch( document.getElementById('ineo-quicksearch-words').value ); 
			return false;
		}
	}
	
	// Article View Event Handlers:
	if( ineoModule == 'article' && ineoAction == 'view' )
	{
		var del; var create; var view; var opts;
		
		if( del = document.getElementById('ineo-delete-article') )
		{
			del.onclick = function() { return ineoConfirmDeleteArticle(); }
		}
		
		if( create = document.getElementById('ineo-topic-create') )
		{
			create.onclick = function() { ineoCreateTopic( document.getElementById('article_id').value ); return false; }
		}
		
		if( view = document.getElementById('ineo-topic-view') ) 
		{
			if( useAjaxPreview == 1 )
			{
				view.onclick = function() { ineoShowTopic( document.getElementById('article_tid').value , document.getElementById('article_id').value ); return false; }
			}
		}
	}
	
	ineoInitRating();
	ineoInitPreview();
	
	// Category View Event Handlers:
	if( ineoModule == 'category' && ineoAction == 'cat' )
	{
		ineoAddReorderEvents();
	}
}

/**
* Quick Search Event Handler!
*/
function ineoQuickSearch( words )
{
	ineoAjaxReplace('op=search&q=' + words,'ineo-search-results',ineo_lang_searching);
}

var reorder_orders = Array();
reorder_orders['article_title']       = 'DESC';
reorder_orders['article_desc']        = 'DESC';
reorder_orders['article_views']       = 'DESC';
reorder_orders['article_date']        = 'DESC';

function ineoAddReorderEvents()
{
	var title = document.getElementById('article_title');
	var desc  = document.getElementById('article_desc');
	var views = document.getElementById('article_views');
	var date  = document.getElementById('article_date');
	
	ineoAddReorderEventHandler( title , 'article_title' );
	ineoAddReorderEventHandler( desc  , 'article_description' );
	ineoAddReorderEventHandler( views , 'article_views' );
	ineoAddReorderEventHandler( date  , 'article_created_date' );
}

function ineoAddReorderEventHandler( element , eleId )
{
	/**
	* I'm not quite sure if it's supposed to be hand, or pointer... so i'ma set both! :D
	*/
	element.cursor = 'hand';
	element.cursor = 'pointer';
	element.style.cursor = 'hand';
	element.style.cursor = 'pointer';

	element.onclick = function () {
		var order = '';
	
		if( reorder_orders[eleId] == 'DESC' )
		{
			order = 'ASC';
			reorder_orders[eleId] = 'ASC';
		}
		else
		{
			order = 'DESC';
			reorder_orders[eleId] = 'DESC';
		}

		ineoDoArticleReorder( eleId , order );
	}
}

function ineoDoArticleReorder( order_by , order_dir )
{
	var urlParams = "op=reorder_articles&category=" + cat + "&order_by=" + order_by + "&order_dir=" + order_dir + "&page=" + startpage;
	
	if( ineoAjaxIsCached(urlParams) )
	{
		ineoAjaxCacheReplace('category-articles-div',urlParams);
		return;
	}
	
	var myEvent = function() {
		
		if( ineoAjaxReady() )
		{
			ineoAjaxReplaceEvent('category-articles-div', ineoAjaxResponseText() );
			ineoAddReorderEvents();
		}
	}
	
	ineoAjaxEvent(urlParams, myEvent, ineo_lang_reordering);
}

function ineoCreateTopic( topic_id )
{
	ineoAjaxReplace('op=create_topic&id=' + topic_id , 'ineo-topic', ineo_lang_creating_topic);
}

function ineoShowTopic( topic_id , article_id )
{
	var urlParams = 'op=topic_quickview&aid=' + article_id + '&tid=' + topic_id;
	
	if( ineoAjaxIsCached(urlParams) )
	{
		ineoAjaxCacheReplace('topic_content',urlParams);
		return;
	}
	
	var myEvent = function() {
		if( ineoAjaxReady() )
		{
			var art = document.getElementById('article_content');
			art.style.display = 'none';
	
			ineoAjaxReplaceEvent('topic_content',ineoAjaxResponseText());
		}
	}	
	
	ineoAjaxEvent(urlParams,myEvent,ineo_lang_loading_topic);
}

function ineoReturnToArticle()
{
	var art = document.getElementById('article_content');
	art.style.display = 'block';
	var dyn = document.getElementById('topic_content');
	dyn.style.display = 'none';
}

function ineoConfirmDeleteArticle( URL )
{
	return confirm( ineo_lang_confirm_delete );
}

function ineoRateArticle( article , score )
{
	var myEvent = function() {
		if( ineoAjaxReady() )
		{	
			ineoAjaxReplaceEvent('ineo-rate-options',ineoAjaxResponseText());
			
			// Make sure the new rating stars are initialized for events!
			ineoInitRating();
		}
	}	
	
	ineoAjaxEvent('op=rate_article&artid=' + article + '&rating=' + score,myEvent,ineo_lang_rating);
}


function ineoInitRating()
{
	if( document.getElementById('ineo-rate-options') ) 
	{
		//----
		// SET UP ONCLICK FUNCTIONS
		//----
		
		document.getElementById('ineo-rate-1').onclick = function()
		{
			ineoRateArticle( document.getElementById('article_id').value , 1 );
			return false;
		}
		
		document.getElementById('ineo-rate-2').onclick = function()
		{
			ineoRateArticle( document.getElementById('article_id').value , 2 );
			return false;
		}
		
		document.getElementById('ineo-rate-3').onclick = function()
		{
			ineoRateArticle( document.getElementById('article_id').value , 3 );
			return false;
		}
		
		document.getElementById('ineo-rate-4').onclick = function()
		{
			ineoRateArticle( document.getElementById('article_id').value , 4 );
			return false;
		}
		
		document.getElementById('ineo-rate-5').onclick = function()
		{
			ineoRateArticle( document.getElementById('article_id').value , 5 );
			return false;
		}
		
		//----
		// SET UP ONMOUSEOVER FUNCTIONS
		//----
		if( document.getElementById('ineo-rate-img-1') )
		{
			document.getElementById('ineo-rate-img-1').onmouseover = function()
			{
				ineoSetSelectedStars( 1 );

				document.getElementById('ineo-rate-img-1').onmouseout = function()
				{
					ineoRestoreRating();
				}

				return false;
			}

			document.getElementById('ineo-rate-img-2').onmouseover = function()
			{
				ineoSetSelectedStars( 2 );

				document.getElementById('ineo-rate-img-2').onmouseout = function()
				{
					ineoRestoreRating();
				}

				return false;
			}

			document.getElementById('ineo-rate-img-3').onmouseover = function()
			{
				ineoSetSelectedStars( 3 );

				document.getElementById('ineo-rate-img-3').onmouseout = function()
				{
					ineoRestoreRating();
				}

				return false;
			}

			document.getElementById('ineo-rate-img-4').onmouseover = function()
			{
				ineoSetSelectedStars( 4 );

				document.getElementById('ineo-rate-img-4').onmouseout = function()
				{
					ineoRestoreRating();
				}

				return false;
			}

			document.getElementById('ineo-rate-img-5').onmouseover = function()
			{
				ineoSetSelectedStars( 5 );

				document.getElementById('ineo-rate-img-5').onmouseout = function()
				{
					ineoRestoreRating();
				}

				return false;
			}
		}
	}
}

function ineoSetSelectedStars( rating )
{
	var star1 = document.getElementById('ineo-rate-img-1');
	var star2 = document.getElementById('ineo-rate-img-2');
	var star3 = document.getElementById('ineo-rate-img-3');
	var star4 = document.getElementById('ineo-rate-img-4');
	var star5 = document.getElementById('ineo-rate-img-5');
	
	staro1 = star1.src;
	staro2 = star2.src;
	staro3 = star3.src;
	staro4 = star4.src;
	staro5 = star5.src;
	
	star1.src = ipb_var_image_url + '/folder_profile_portal/star_empty.gif';
	star2.src = ipb_var_image_url + '/folder_profile_portal/star_empty.gif';
	star3.src = ipb_var_image_url + '/folder_profile_portal/star_empty.gif';
	star4.src = ipb_var_image_url + '/folder_profile_portal/star_empty.gif';
	star5.src = ipb_var_image_url + '/folder_profile_portal/star_empty.gif';
	
	if( rating > 0 )
	{
		star1.src = ipb_var_image_url + '/folder_profile_portal/star_selected.gif';
	}
	
	if( rating > 1 )
	{
		star2.src = ipb_var_image_url + '/folder_profile_portal/star_selected.gif';
	}
	
	if( rating > 2 )
	{
		star3.src = ipb_var_image_url + '/folder_profile_portal/star_selected.gif';
	}
	
	if( rating > 3 )
	{
		star4.src = ipb_var_image_url + '/folder_profile_portal/star_selected.gif';
	}
	
	if( rating > 4 )
	{
		star5.src = ipb_var_image_url + '/folder_profile_portal/star_selected.gif';
	}
}

function ineoRestoreRating()
{
	var star1 = document.getElementById('ineo-rate-img-1');
	var star2 = document.getElementById('ineo-rate-img-2');
	var star3 = document.getElementById('ineo-rate-img-3');
	var star4 = document.getElementById('ineo-rate-img-4');
	var star5 = document.getElementById('ineo-rate-img-5');
	
	star1.src = staro1;
	star2.src = staro2;
	star3.src = staro3;
	star4.src = staro4;
	star5.src = staro5;
}

function ineoInitPreview()
{
	var prev;
	
	if( prev = document.getElementById('btn-ajax-preview') )
	{
		prev.onclick = function()
		{
			// get RTE contents:
			var content = ineoGetRTEContent();
			
			// make an array of it:
			var arr_content        = new Array(); 
			arr_content['preview'] = content;
			
			// make our response function:
			var myEvent = function()
			{
				if( ineoAjaxReady() )
				{
					var preview = document.getElementById('content-ajax-preview');
					preview.style.display = '';
					
					preview.innerHTML = ineoAjaxEvent.xmlhandler.responseText;
				}
			}
			
			// set up Ajax POST request:
			ineoAjaxRequestType = 'POST';
			ineoAjaxPostData    = ineoAjaxRequest.format_for_post(arr_content);
			
			// Run:
			ineoAjaxEvent("op=article_preview",myEvent,'LOADING PREVIEW');
		}
	}
}

function ineoGetRTEContent()
{
	return "This doesn't work yet!";
}
